package com.pig.controller;

import com.pig.entity.AccountUserEntity;
import com.pig.entity.AccountUserInfoEntity;
import com.pig.pojo.AccounInfoPojo;
import com.pig.service.AccountInfoService;
import com.pig.service.AccountUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 查询用户信息
 */
@Controller
public class AccountInfoController {
 /* 隐藏手机号码
    String phone = "18156225251";
    String phoneNumber = phone.substring(0, 3) + "****" + phone.substring(7, phone.length());
        System.out.println("phone总长度：" + phone.length());
        System.out.println("phone前3位：" + phone.substring(0, 3));
        System.out.println("phone中4位：" + phone.substring(3, 7));
        System.out.println("phone后4位：" + phone.substring(7, phone.length()));
        System.out.println("phone中4*：" + phoneNumber);
*/
    @Autowired
    private AccountInfoService accountInfoService;


    @Autowired
    private AccountUserService accountUserService;

    /**
     * 查询用户基本信息
     */
    @ResponseBody
    @RequestMapping("/user/accountInfo")
    public Map<String ,Object> queryAccountInfo(HttpServletRequest request, HttpSession session){
        Map<String,Object> map = new HashMap<String, Object>();
        System.out.println("------------进入查询用户信息模式-------------");
        String accountName=(String) session.getAttribute("accountName");
        AccountUserEntity accountUserEntity=new AccountUserEntity();
        map.put("statusCode","200");
        map.put("accountName",accountName);
        AccounInfoPojo accounInfoPojo=new AccounInfoPojo();
        accounInfoPojo = accountInfoService.queryAccountInfo(map);
        System.out.println("accounInfoPojo-- "+accounInfoPojo);
        System.out.println("性别-- "+accounInfoPojo.getAccountInfoSex());
        if (null==accounInfoPojo){
            map.put("failed","500");
            return map;
        }

        //手机号码
        String phone=accounInfoPojo.getAccountInfoPhone();
        String phoneNumber = phone.substring(0, 3) + "****" + phone.substring(7, phone.length());
        System.out.println(phone+" -- "+phoneNumber);
        //邮箱
        String email=accounInfoPojo.getAccountInfoEmail();
        String hiddenEmail = email.replaceAll("(\\w?)(\\w+)(\\w)(@\\w+\\.[a-z]+(\\.[a-z]+)?)","$1***$3$4");
        System.out.println(email+" -- "+hiddenEmail);
        //生日
        String hiddenAccountInfoBirthday=accounInfoPojo.getAccountInfoBirthday();
        System.out.println("生日-- "+hiddenAccountInfoBirthday);
        Map<String,Object> map2 = new HashMap<String, Object>();
        map2.put("map",map);

        String accountSex=accounInfoPojo.getAccountInfoSex();
        String accountInfoImage=accounInfoPojo.getAccountInfoImage();

        System.out.println("性别 -- "+accountSex);

        if (null==accountSex){
            map2.put("noSex","404");
        }

        if ("保密".equals(accountSex)){
            map2.put("noSex","404");
        }

        System.out.println("-----保密----");
        if ("男".equals(accountSex)){
            map2.put("man","300");
        }
        System.out.println("-----男----");

        if ("女".equals(accountSex)){
            map2.put("woman","380");
        }
        System.out.println("-----女----");
        map2.put("accountName",accountName);
        map2.put("accountInfoImage",accountInfoImage);
        map2.put("phoneNumber",phone);
        map2.put("hiddenEmail",email);
        map2.put("hiddenAccountInfoBirthday",hiddenAccountInfoBirthday);

        System.out.println("map2-- "+map2);

        return map2;

    }


    /**
     * 修改用户信息
     */
    @ResponseBody
    @RequestMapping("/user/updateAccountInfoBirthday")
    public String updateAccountInfoBirthday(HttpServletRequest request, HttpSession session){
        System.out.println("--------------------进入修改信息模式--------------------");
        String accountInfoBirthday=request.getParameter("accountInfoBirthday");
        String phoneNumber=request.getParameter("phoneNumber");
        String hiddenEmail=request.getParameter("hiddenEmail");

        System.out.println(accountInfoBirthday+" -- "+phoneNumber+" -- "+hiddenEmail);
        AccountUserEntity accountUserEntity=new AccountUserEntity();
        String accountName=(String) session.getAttribute("accountName");
        accountUserEntity = accountUserService.selectName(accountName);
        Integer accountInfoId=accountUserEntity.getAccountId();

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("accountInfoBirthday",accountInfoBirthday);
        map.put("accountInfoId",accountInfoId);
        map.put("accountInfoPhone",phoneNumber);
        map.put("accountInfoEmail",hiddenEmail);


        int flag = accountInfoService.updateAccountInfoBirthday(map);
        System.out.println(flag);
        if (flag>0){
            System.out.println("用户信息修改成功");
            return "200";
        }
        System.out.println("用户信息修改失败");
        return "500";
    }

    /**
     * 修改用户头像
     *
     */
    @ResponseBody
    @RequestMapping("/user/updateAccountInfoImage")
    public Object headImg(@RequestParam(value="file",required=false) MultipartFile file, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
        long startTime = System.currentTimeMillis();
        String prefix = "";
        String dateStr = "";
        Map<String, Object> map = new HashMap<String, Object>();
        String accountName=(String) session.getAttribute("accountName");
        AccountUserEntity accountUserEntity=new AccountUserEntity();
        accountUserEntity = accountUserService.selectName(accountName);
        Integer accountInfoId=accountUserEntity.getAccountId();

        System.out.println(System.getProperty("catalina.home"));

        //保存上传
        String strTime = Long.toString(startTime);
        OutputStream out = null;
        InputStream fileInput = null;
        String filepath = null;
        try {
            if (file != null) {
                String originalName = file.getOriginalFilename();
                prefix = originalName.substring(originalName.lastIndexOf(".") + 1);
                System.out.println("prefix--" + prefix);
                System.out.println("dateStr--" + dateStr);
                System.out.println("originalName--" + originalName);

                System.out.println("文件原名称--" + file.getOriginalFilename());
                System.out.println("文件大小--" + file.getSize());
                System.out.println("文件类型--" + file.getContentType());

                filepath = request.getServletContext().getRealPath("/")+"uploadImage/";

                filepath = filepath.replace("\\", "/");

                Long size = file.getSize();
                String strSize = Long.toString(size);

                System.out.println("filepath-- "+filepath);
                File files = new File(filepath+strTime+"."+prefix);

                //打印查看上传路径
                System.out.println("files-- " + files);
                if (!files.getParentFile().exists()) {
                    files.getParentFile().mkdirs();
                }
                file.transferTo(files);

            }
        } catch (Exception e) {
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (fileInput != null) {
                    fileInput.close();
                }
            } catch (IOException e) {
            }
        }

        Map<String, Object> map2 = new HashMap<String, Object>();

        map.put("code", 0);
        map.put("msg", "");
        map.put("data", map2);
        map2.put("src",request.getContextPath()+"/uploadImage/"+ strTime+dateStr + "." + prefix);
        map.put("accountInfoId",accountInfoId);
        map.put("accountInfoImage", request.getContextPath()+"/uploadImage/"+strTime+"."+prefix);
        //保存图片路径到数据库
        int flag =accountInfoService.updateAccountInfoBirthday(map);
        System.out.println(flag);
        if (flag>0) {
            System.out.println("头像修改成功");
        } else {
            System.out.println("头像修改失败");
        }
        // 运行成功之后的时间
        long endTime = System.currentTimeMillis();
        float excTime = (float) (endTime - startTime) / 1000;
        System.out.println("执行时间：" + excTime + "s");
        return map;
    }


    /**
     * 修改用户性别
     */
    @ResponseBody
    @RequestMapping("/user/updateAccountInfoSex")
    public String updateAccountInfoSex(HttpServletRequest request, HttpSession session){
        System.out.println("--------------------进入修改性别模式--------------------");

        String accountInfoSex=request.getParameter("accountInfoSex");

        System.out.println("accountInfoSex-- "+accountInfoSex);
        AccountUserEntity accountUserEntity=new AccountUserEntity();
        String accountName=(String) session.getAttribute("accountName");
        accountUserEntity = accountUserService.selectName(accountName);
        Integer accountInfoId=accountUserEntity.getAccountId();

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("accountInfoSex",accountInfoSex);
        map.put("accountInfoId",accountInfoId);

        int flag = accountInfoService.updateAccountInfoBirthday(map);
        System.out.println(flag);
        if (flag>0){
            System.out.println("性别修改成功");
            return "200";
        }
        System.out.println("性别修改失败");
        return "500";
    }

}
